{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from jqdata import *\n",
    "import pandas as pd\n",
    "from datetime import datetime as dt\n",
    "import talib as tl\n",
    "import gc\n",
    "\n",
    "from jqlib.technical_analysis import *\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 聚宽自带EMA(本地不能运行)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'TF9999.CCFX': 99.8507059607845}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "security_list1 = 'TF9999.CCFX'\n",
    "date_str = '2021-06-11'\n",
    "EMA1 = EMA(security_list1, check_date=date_str, timeperiod=14, include_now=True)\n",
    "EMA1\n",
    "# tupo = EMA1[security_list1]\n",
    "# A1X = (EMA(security_list1, check_date='2021-06-10', timeperiod=10)[security_list1] - tupo)/tupo*100\n",
    "# A1X"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# talib EMA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2021-05-14    100.205\n",
       "2021-05-17    100.150\n",
       "2021-05-18    100.135\n",
       "2021-05-19    100.275\n",
       "2021-05-20    100.415\n",
       "2021-05-21    100.155\n",
       "2021-05-24    100.065\n",
       "2021-05-25    100.085\n",
       "2021-05-26    100.105\n",
       "2021-05-27    100.010\n",
       "2021-05-28     99.960\n",
       "2021-05-31    100.015\n",
       "2021-06-01    100.035\n",
       "2021-06-02     99.905\n",
       "2021-06-03     99.975\n",
       "2021-06-04     99.800\n",
       "2021-06-07     99.680\n",
       "2021-06-08     99.705\n",
       "2021-06-09     99.690\n",
       "2021-06-10     99.765\n",
       "Name: close, dtype: float64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# close_s = attribute_history(security_list1, 14, unit='1d', fields=['close'],df=True)['close']#\n",
    "close_s = get_price(security_list1, count = 20, end_date='2021-06-10', frequency='daily', fields=['open','close'])\n",
    "close_s.iloc[:,-1]\n",
    "# tl.EMA(close_s, 14) #若日期<close_s的，前n个是NaN\n",
    "# tl.EMA(close_s, 14)[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fit_series(*series_list):\n",
    "    size = min(len(series) for series in series_list)\n",
    "    print('size:', str(size))\n",
    "    if size == 0:\n",
    "        raise Exception(\"series size == 0\")\n",
    "    new_series_list = [series[-size:] for series in series_list]\n",
    "    return new_series_list\n",
    "\n",
    "def CrossOver(series1, series2):\n",
    "    \"\"\"s1金叉s2\n",
    "    :param s1:\n",
    "    :param s2:\n",
    "    :returns: bool序列\n",
    "    \"\"\"\n",
    "    cond1 = series1 > series2\n",
    "    series1, series2 = fit_series(series1[1].series, series2[1].series)\n",
    "    cond2 = series1 <= series2  # s1[1].series <= s2[1].series\n",
    "    cond1, cond2 = fit_series(cond1, cond2)\n",
    "    s = cond1 & cond2\n",
    "    return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "invalid index to scalar variable.",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-12-45346aeb7e74>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mCrossOver\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mA1X\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m<ipython-input-11-03d89b246d9c>\u001b[0m in \u001b[0;36mCrossOver\u001b[0;34m(series1, series2)\u001b[0m\n\u001b[1;32m     14\u001b[0m     \"\"\"\n\u001b[1;32m     15\u001b[0m     \u001b[0mcond1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mseries1\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0mseries2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m     \u001b[0mseries1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mseries2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfit_series\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mseries1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mseries\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mseries2\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mseries\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     17\u001b[0m     \u001b[0mcond2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mseries1\u001b[0m \u001b[0;34m<=\u001b[0m \u001b[0mseries2\u001b[0m  \u001b[0;31m# s1[1].series <= s2[1].series\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     18\u001b[0m     \u001b[0mcond1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcond2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfit_series\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcond1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcond2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mIndexError\u001b[0m: invalid index to scalar variable."
     ]
    }
   ],
   "source": [
    "CrossOver(A1X, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "MarkDown菜单",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
